package com.wzz.seckillone.activeMq.base;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @ProjectName: seckillOne
 * @Package: com.wzz.seckillone.activeMq
 * @ClassName: TopicConsumer
 * @Author: wzz
 * @Description: 消息消费者
 * @Date: 2022/3/20 14:50
 * @Version: 1.0
 */
public class TopicConsumer1 {
    private static final String ACTIVEMQ_URL = "tcp://192.168.1.104:61616";
    private static final String topicName = "topicOne";
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = connectionFactory.createConnection();
        connection.setClientID("li4");
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(topicName);
        TopicSubscriber sub = session.createDurableSubscriber(topic, "remark something");
        Message receive = sub.receive();
        while(receive != null){
            TextMessage message = (TextMessage) receive;
            System.out.println(message.getText());
            receive = sub.receive(5000l);
        }
        sub.close();
        session.close();
        connection.close();
    }
}
